<template>
  <div class="login-container">
    <el-card>
      <div slot="header">
        <span>分布式秒杀系统-用户登录</span>
      </div>

      <el-form ref="form" :model="form" :rules="formRules" label-width="80px" label-suffix="：">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" style="width: 250px"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" style="width: 250px"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit()">登录</el-button>
          <el-button type="primary" @click="handleReset()">重置</el-button>
        </el-form-item>
      </el-form>

      <div style="text-align: center">
        版权所有：<a href="http://www.baidu.com">www.gaoxu.vip</a>
      </div>
    </el-card>
  </div>
</template>

<script>
import {hex_md5} from "@/util/md5";

export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: '',
      },
      formRules: {
        username: [
          {required: true, message: '用户名不能为空！', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '密码不能为空！', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let data = {"username": this.form.username, "password": hex_md5(this.$sysConfig.salt + this.form.password)}
          this.$axios.post("user/login", data).then(response => {
            let data = response.data
            if (data.code === 500) {
              this.$message.warning(data.msg)
            } else if (data.code === 0) {
              window.sessionStorage.setItem("token", data.msg)
              this.$router.replace("/main")
            }
          }).catch(error => {
            this.$message.warning(error)
          })
        } else {
          this.$message.warning("请根据提示输入正确用户信息")
        }
      })
    },
    handleReset(){
      this.$refs.form.resetFields()
    }
  }
}
</script>


<style scoped>

.login-container {
  display: flex;
  justify-content: center;
  margin-top: 150px;
}

</style>
